{% extends "ui/base-devices.html" %}
{% load url from future %}
{% load icons %}
{% load bob %}

{% block content %}
<div class="row-fluid">
    <div class="span6">
        <h3>DNS Records</h3>
        <form class="form form-inline" action="" method="POST">
            {% csrf_token %}
            {{ dnsformset.management_form }}
            <table class="table table-striped table-bordered details-dns table-condensed">
                <thead><tr>
                    <th width="16"></th>
                    <th width="30%">Name</th>
                    <th width="72">Type</th>
                    <th width="*">Content</th>
                    <th width="16">PTR</th>
                    <th width="16">Del</th>
                </tr></thead>
                <tbody>
                    {% for form in dnsformset %}
                    <tr>
                        <td style="vertical-align:middle">
                            {% if form.instance.content %}
                                {% icon 'fugue-price-tag' %}
                            {% else %}
                                {% icon 'fugue-price-tag--plus' %}
                            {% endif %}
                            {% for f in form.hidden_fields %}{{ f }}{% endfor %}
                        </td>
                        {% for f in form.visible_fields %}
                        <td class="control-group {{ f.css_classes }} {% if f.errors %}error{% endif %}" style="vertical-align:middle">
                            {% if form.instance.content or not 'DELETE' in f.name %}
                                {% if canedit %}
                                {{ f }}
                                {% else %}
                                {{ f.value|default:'' }}
                                {% endif %}
                                {% for e in f.errors %}
                                    <div class="help-block">{{ e }}</div>
                                {% endfor %}
                            {% endif %}
                        </td>
                        {% endfor %}
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
            {% if canedit %}
                <div class="buttons pull-right">
                    {% spaceless %}
                    <button type="submit" name="dns" value="Save" class="btn btn-primary"
                    >{% icon 'fugue-tick' %}&nbsp;Save</button>
                    {% endspaceless %}
                </div>
            {% endif %}
        </form>

        <h3>DHCP Entries</h3>
        <form class="form form-inline" action="" method="POST">
            {% csrf_token %}
            {{ dhcpformset.management_form }}
            <table class="table table-striped table-bordered table-condensed details-dhcp">
                <thead><tr>
                    <th width="16"></th>
                    <th>IP Address</th>
                    <th>MAC Address</th>
                    <th width="16">Del</th>
                </tr></thead>
                <tbody>
                    {% for form in dhcpformset %}
                    <tr>
                        <td style="vertical-align:middle">
                            {% icon 'fugue-network-ethernet' %}
                            {% for f in form.hidden_fields %}{{ f }}{% endfor %}
                        </td>
                    {%   for f in form.visible_fields %}
                    <td class="control-group {{ f.css_classes }} {% if f.errors %}error{% endif %}" style="vertical-align:middle">
                        {% if form.instance.ip or not 'DELETE' in f.name %}
                            {% if canedit %}
                            {{ f }}
                            {% else %}
                            {{ f.value|default:'' }}
                            {% endif %}
                            {% for e in f.errors %}
                                <div class="help-block">{{ e }}</div>
                            {% empty %}
                            {%  if f.help_text %}
                                <div class="help-block">{{ f.help_text }}</div>
                            {%  endif %}
                            {% endfor %}
                        {% endif %}
                    </td>
                    {%   endfor %}
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
            {% if canedit %}
                <div class="buttons pull-right">
                    {% spaceless %}
                    <button type="submit" name="dhcp" value="Save" class="btn btn-primary"
                    >{% icon 'fugue-tick' %}&nbsp;Save</button>
                    {% endspaceless %}
                </div>
            {% endif %}
        </form>
    </div>

    <div class="span6">
        <h3>Management address</h3>
        {% form ip_management_form fugue_icons='fugue-tick' submit_name='management' %}

        <h3>Detected addresses</h3>
        <form class="form form-inline" action="" method="POST">
            {% csrf_token %}
            {{ ipformset.management_form }}
            <table class="table table-striped table-bordered table-condensed details-address">
                <thead><tr>
                    <th width="16"></th>
                    <th width="35%">Hostname</th>
                    <th width="35%">Address</th>
                    <th width="*">Last Ping</th>
                    <th width="16">Del</th>
                </tr></thead>
                <tbody>
                    {% for form in ipformset %}
                    <tr>
                        <td style="vertical-align:middle">
                            {% if form.instance.address %}
                                {{ form.instance|address_icon }}
                            {% else %}
                                {% icon 'fugue-plus' %}
                            {% endif %}
                            {% for f in form.hidden_fields %}{{ f }}{% endfor %}
                        </td>
                    {%   for f in form.visible_fields %}
                    {%      if 'DELETE' in f.name %}
                        <td>{{ form.instance.last_seen|timesince_limited }}</td>
                    {%      endif %}
                    <td class="control-group {{ f.css_classes }} {% if f.errors %}error{% endif %}" style="vertical-align:middle">
                        {% if form.instance.address or not 'DELETE' in f.name %}
                            {% if canedit %}
                            {{ f }}
                            {% else %}
                            {{ f.value|default:'' }}
                            {% endif %}
                            {% for e in f.errors %}
                                <div class="help-block">{{ e }}</div>
                            {% empty %}
                            {% endfor %}
                        {% endif %}
                        {% if 'address' in f.name %}
                        <div class="error-text">
                            {{ form.non_field_errors }}
                        </div>
                        {% endif %}

                    </td>
                    {%   endfor %}
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
            <div class="action-list">
                {% if next_hostname %}
                <div class="alert alert-info">Next available hostname in this DC: {{ next_hostname }}</div>
                {% endif %}
                {% for item in first_free_ip_addresses %}
                <div class="alert alert-info">First free IP for network {{ item.network_name }}: {{ item.first_free_ip }}</div>
                {% endfor %}
            </div>
            {% if canedit %}
                <div class="buttons pull-right">
                    {% spaceless %}
                    <button type="submit" name="ip" value="Save" class="btn btn-primary"
                    >{% icon 'fugue-tick' %}&nbsp;Save</button>
                    {% endspaceless %}
                </div>
            {% endif %}
        </form>

        {% if balancers %}
            <h3>Load balancers</h3>
            <table class="table table-striped table-bordered details-balancers">
                <thead><tr>
                        <th width="16"></th>
                    <th>Load Balancer</th>
                    <th>Pool</th>
                    <th>IP Address</th>
                    <th>Port</th>
                </tr></thead>
                <tbody>
                    {% for item in balancers %}
                    <tr>
                        <td>
                            {% if item.server %}
                                {% icon 'fugue-balance' %}
                            {% else %}
                            {%  if item.enabled %}
                                {% icon 'fugue-tick-circle' %}
                            {%  else %}
                                {% icon 'fugue-cross-circle' %}
                            {%  endif %}
                            {% endif %}
                        </td>
                        <td>
                            {% if item.server %}
                                {{ item.server }}
                            {% else %}
                                {{ item.balancer }}
                            {% endif %}
                        </td>
                        <td>{{ item.pool }}</td>
                        <td>{{ item.address }}</td>
                        <td>{{ item.port }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        {% endif %}
    </div>
</div>
{% endblock content %}
